package com.carrentalplatform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.carrentalplatform.model.Vehicle;
import com.carrentalplatform.service.VehicleService;
import com.carrentalplatform.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/vehicle")
public class VehicleController {

    @Autowired
    private VehicleService vehicleService;

    /**
     * 分页查询可租车辆
     */
    @GetMapping("/list")
    public Result<IPage<Vehicle>> listVehicles(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String brand,
            @RequestParam(required = false) String model,
            @RequestParam(required = false) Integer minPrice,
            @RequestParam(required = false) Integer maxPrice) {
        
        IPage<Vehicle> vehiclePage = vehicleService.pageAvailableVehicles(
                page, size, brand, model, minPrice, maxPrice);
        
        return Result.success(vehiclePage);
    }

    /**
     * 获取车辆详情
     */
    @GetMapping("/{id}")
    public Result<Vehicle> getVehicleDetail(@PathVariable Long id) {
        Vehicle vehicle = vehicleService.getById(id);
        if (vehicle == null) {
            return Result.fail("车辆不存在");
        }
        
        return Result.success(vehicle);
    }

    /**
     * 添加车辆
     */
    @PostMapping("/add")
    public Result<Long> addVehicle(@RequestBody @Valid Vehicle vehicle) {
        if (vehicle.getStatus() == null) {
            return Result.fail("车辆状态不能为空");
        }

        // 保存车辆并返回ID
        boolean success = vehicleService.save(vehicle);
        if (!success) {
            return Result.fail("添加失败");
        }

        return Result.success(vehicle.getId()); // 返回新增的车辆ID
    }

    /**
     * 更新车辆信息
     */
    @PostMapping("/{id}")
    public Result<Void> updateVehicle(@PathVariable Long id, @RequestBody Vehicle vehicle) {
        // 确保ID一致
        vehicle.setId(id);
        
        boolean success = vehicleService.updateById(vehicle);
        if (!success) {
            return Result.fail("更新车辆信息失败");
        }
        
        return Result.success();
    }

    /**
     * 更新车辆状态
     */
    @PutMapping("/status/{id}")
    public Result<Void> updateVehicleStatus(
            @PathVariable Long id, 
            @RequestParam Integer status) {
        
        boolean success = vehicleService.updateVehicleStatus(id, status);
        if (!success) {
            return Result.fail("更新车辆状态失败");
        }
        
        return Result.success();
    }

    /**
     * 更新车辆位置
     */
    @PutMapping("/location/{id}")
    public Result<Void> updateVehicleLocation(
            @PathVariable Long id, 
            @RequestParam String location) {
        
        boolean success = vehicleService.updateVehicleLocation(id, location);
        if (!success) {
            return Result.fail("更新车辆位置失败");
        }
        
        return Result.success();
    }

    /**
     * 获取车行的所有车辆
     */
    @GetMapping("/shop/{shopId}")
    public Result<List<Vehicle>> getShopVehicles(@PathVariable Long shopId) {
        List<Vehicle> vehicles = vehicleService.listByShopId(shopId);
        return Result.success(vehicles);
    }
}